﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using DEL.Webmonitor;
using SFL;

namespace BLL.Webmonitor
{
    public class GetInfosForTestWebsiteJKBLL
    {

        #region 获取原始信息

        /// <summary>
        /// 通过正则表达式从网页源码中获取原始信息集合，如果网站配置要求保存原始信息，则在此方法内提供保存到数据库
        /// </summary>
        public static List<InfoOriginal> GetListOriginal(WebsiteJK websiteJK)
        {
            string tmpStr = string.Format(websiteJK.RegexList);       //导入正则表达式
            MatchCollection matches = Regex.Matches(GetBlockStr(websiteJK), tmpStr, RegexOptions.IgnoreCase);
            List<InfoOriginal> lit = new List<InfoOriginal>();      ////定义集合对象
            try
            {
                if (matches.Count > 0)
                {
                    for (int i = 0; i < matches.Count; i++)
                    {
                        string title = matches[i].Groups["title"].Value.Trim();
                        title = HtmlRemoval.StripTagsCharArray(title); 
                        string url = websiteJK.PrefixUrl + matches[i].Groups["url"].Value.Trim();                        
                        string author = matches[i].Groups["author"].Value.Trim();
                        string summary = matches[i].Groups["summary"].Value.Trim();
                        summary = HtmlRemoval.StripTagsCharArray(summary);
                        //此处是针对微博信息，没有标题，只能从内容中提取前面若干字符作为标题
                        if (title.Length < 1 && summary.Length > 1)
                        {
                            if (summary.Length > 100)
                                title = summary.Substring(0, 100);
                            else
                                title = summary;
                        }                        
                        string public_time = matches[i].Groups["public_time"].Value.Trim();
                        InfoOriginal infoOriginal = new InfoOriginal() { WebsiteTypeId = websiteJK.WebsiteTypeId, WebsiteId = websiteJK.WebsiteId, BigclassId = websiteJK.Id, CreatedOn = DateTime.Now.ToLocalTime(), Url = url, Title = title, Author = author, Summary = summary, PublicTime = public_time };
                        lit.Add(infoOriginal);                            
                    }
                }
            }
            catch (Exception ee)
            {
                throw new CustomException("获取源码中信息列表错误。\r\n错误信息：" + ee.Message);                
            }
            return lit;
        }

        //网页源码中包含贴子列表的一个大区块（正则表达式），如未能匹配，返回网页源码
        public static string GetBlockStr(WebsiteJK website)
        {
            string orgStr = HTTPHelper.GetHTMLByURL(website.BigclassUrl, website.Coding);
            if (website.Decryption == "unicode")
            {
                orgStr = GB2312UnicodeConverter.ToGB2312(orgStr);
            }
            //MessageBox.Show(tmpStr);  

            if (website.RegexBlock != null)
            {
                string tmpStr = string.Format(website.RegexBlock);
                MatchCollection matches = Regex.Matches(orgStr, tmpStr, RegexOptions.IgnoreCase);
                string result = orgStr;
                try
                {
                    result = matches[0].Value.Trim();
                }
                catch (Exception ee)
                {
                    throw new CustomException("获取源码中区块错误。\r\n错误信息：" + ee);
                }                
                return result;
            }
            else
            {
                return orgStr;
            }

        }

        #endregion

    }
}
